package com.das.mechanic_base.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3ScreenUtils;

public class X3CameraTakeView extends View implements View.OnClickListener {

    private Context mContext;

    private Paint mPaint;

    private Paint tPaint;

    /**
     * 中心圆的半径
     */
    private int mRadius = 0;

    /**
     * 外层圆半径
     */
    private int oRadius = 0;

    private int startColor;

    private int endColor;

    private int radius;

    /**
     * 当前的状态是视频还是照片
     */
    private int modeIndex = 0;

    public X3CameraTakeView(Context context) {
        this(context,null);
    }

    public X3CameraTakeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    private void initView() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);

        tPaint = new Paint();
        tPaint.setAntiAlias(true);
        tPaint.setStyle(Paint.Style.FILL);

        startColor = Color.WHITE;
        endColor = ContextCompat.getColor(mContext, R.color._999999);

        radius = X3ScreenUtils.dipToPx(31,mContext);
        this.setOnClickListener(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(startColor);
        canvas.drawCircle(radius, radius, oRadius,mPaint);
        tPaint.setColor(endColor);
        canvas.drawCircle(radius, radius,mRadius,tPaint);
    }

    /**
     * 视频向拍照转换
     */
    public void videoToPicture() {
        /**
         * 首先要将背景view的颜色转换为灰色 #999999
         * 然后将外层view的颜色变为白色 并且半径给个动画
         */
        modeIndex = 1;
        startColor = ContextCompat.getColor(mContext, R.color._999999);
        endColor = Color.WHITE;

        oRadius = radius;
        ValueAnimator animator = ValueAnimator.ofInt(radius,radius - X3ScreenUtils.dipToPx(5,mContext));
        animator.setDuration(400);
        animator.addUpdateListener(animation -> {
            mRadius = (int) animation.getAnimatedValue();
            invalidate();
        });
        animator.start();

    }

    /**
     * 拍照向视频转换
     */
    public void pictureToVideo() {
        /**
         * 同视频转拍照 对调
         */
        modeIndex = 0;
        startColor = Color.WHITE;
        endColor = ContextCompat.getColor(mContext, R.color._999999);

        ValueAnimator oAnim = ValueAnimator.ofInt(radius - X3ScreenUtils.dipToPx(3,mContext),radius);
        oAnim.setDuration(400);
        oAnim.addUpdateListener(animation -> {
            oRadius = (int) animation.getAnimatedValue();
            invalidate();
        });
        oAnim.start();

        ValueAnimator mAnim = ValueAnimator.ofInt(radius - X3ScreenUtils.dipToPx(5,mContext),radius - X3ScreenUtils.dipToPx(3,mContext));
        mAnim.setDuration(200);
        mAnim.addUpdateListener(animation -> {
            mRadius = (int) animation.getAnimatedValue();
        });
        mAnim.start();
    }

    @Override
    public void onClick(View v) {
        if (iOnClickTake == null) {
            return;
        }
        if (modeIndex == 0) {
            iOnClickTake.iOnClickVideo();
        }else {
            iOnClickTake.iOnClickPicture();
        }
    }

    IOnClickTake iOnClickTake;

    public void setiOnClickTake(IOnClickTake iOnClickTake) {
        this.iOnClickTake = iOnClickTake;
    }

    public interface IOnClickTake {

        void iOnClickVideo();

        void iOnClickPicture();
    }
}
